<?php

/**
 * 分享
 * Class share
 */
class share {

    const SUCCESS = 1;//成功
    const HAS = 2;//已点赞

    private $db_content;
    private $db_share;

    public function  __construct() {
        $this->db_content = pc_base::load_model('content_model');
        $this->db_share = pc_base::load_model('share_model');
    }

    //计数
    public function add() {
        $uid = (int)param::get_cookie('_userid');
        $catid = $_POST['catid'] ? (int)$_POST['catid'] : 0;
        $id = $_POST['id'] ? (int)$_POST['id'] : 0;
        $pc = $_POST['pc'] ? safe_replace($_POST['pc']) : '';
        $ymd = date('Y-m-d');

        //查是否已有
        $sql = "uid='$uid' and a_catid='$catid' and a_id='$id' and ymd='$ymd'";
        $res = $this->db_share->get_one($sql);

        if ($res) {
            json_exit(self::HAS, 100, '您已经分享过了');
        } else {
            //记录
            $this->db_share->insert([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
                'ymd' => date('Y-m-d'),
                'ymdhis' => date('Y-m-d H:i:s'),
                'pc' => $pc,
            ]);

            //更新数量
            $MODEL = getcache('model', 'commons');
            $CATEGORYS = getcache('category_content_1', 'commons');
            $modelid = $CATEGORYS[$catid]['modelid'];
            $table = $MODEL[$modelid]['tablename'];
            $this->db_content->table_name = 'v9_' . $table;
            $sql = "update {$this->db_content->table_name} set num_share=num_share+1 where id=$id";
            $this->db_content->query($sql);

            //加分 5
            $db_member = pc_base::load_model('member_model');
            $cha = $db_member::log($uid, 5, $db_member::TYPE_SHARE, "分享-$catid-$id", __METHOD__);

            json_exit(self::SUCCESS);
        }
    }

}